﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL = KC.Business.Web.MV8;
using DAL = KC.DataAccess.MV;

public partial class Gallery : System.Web.UI.Page
{
	int parentId = -1;

	protected void Page_Load(object sender, EventArgs e)
    {
		DAL.MV8DataContext dc = new DAL.MV8DataContext(System.Configuration.ConfigurationManager.ConnectionStrings["MV8"].ConnectionString);
		if (Request.QueryString["ParentId"] != null)
			int.TryParse(Request.QueryString["ParentId"], out parentId);
		IQueryable<DAL.Gallery> results =
			from g in dc.Galleries
			where g.ParentId == (parentId == -1 ? 0 : parentId)
			orderby g.id descending
			select g;

		// List<DAL.Gallery> results2 = results.ToList<DAL.Gallery>();
		//if (parentId > 0) {
		//    results2.Insert(0, new DAL.Gallery() { id = -1, Filename = null, ParentId = parentId, DisplayName = "Up one Level" });
		//    results2.Insert(0, new DAL.Gallery() { id = -1, Filename = null, ParentId = -1, DisplayName = "Top" }); }

		rGallery.DataSource = results;
		rGallery.DataBind();
		BuildBreadcrumbBar(parentId);
    }
	protected void rGallery_ItemDataBound(object sender, RepeaterItemEventArgs e)
	{
		if (e.Item == null || e.Item.DataItem == null) return;
		DAL.Gallery g = (DAL.Gallery)e.Item.DataItem;

		// Wrap every few cells
		if (e.Item.ItemIndex == 47 || (e.Item.ItemIndex > 0 && (e.Item.ItemIndex - 2) % 3 == 0))
			e.Item.Controls.Add(new LiteralControl("</td></tr><tr><td style=\"vertical-align:bottom;\">"));
		else e.Item.Controls.Add(new LiteralControl("</td><td style=\"vertical-align:bottom;\">"));

		((Label)e.Item.FindControl("lblName")).Text = g.DisplayName;

		HyperLink hlImg = (HyperLink)e.Item.FindControl("hlImg");

		hlImg.NavigateUrl = BLL.MapGallery(string.IsNullOrEmpty(g.Filename) ? "Gallery.aspx?ParentId="+g.id.ToString(): g.Filename);
		if (!string.IsNullOrEmpty(g.Filename) && g.Filename.Length > 0) hlImg.Target = "_blank";
		hlImg.Attributes.Remove("rel");
		hlImg.Attributes.Add("rel", "canonical");

		Image img = (Image)e.Item.FindControl("img");
		img.ImageUrl = g.Galleries.Count == 0 && !string.IsNullOrEmpty(g.Filename)
			? BLL.MapGallery(g.ThumbnailFilename)
			: "Images/ImagesFolder.png";
		hlImg.NavigateUrl = g.Galleries.Count == 0 && !string.IsNullOrEmpty(g.Filename)
			? BLL.MapGallery(g.Filename)
			: g.ParentId < 0
				? "Gallery.aspx"
				: "Gallery.aspx?ParentId=" + g.id.ToString();

		img.Width = 150;
		img.ToolTip =
			g.DisplayName + "\n" +
			g.Width + " x " + g.Height + "   (" + ((double)((((double)g.Width * (double)g.Height) / 1000000))).ToString("###,###.#") + " Megapixels) \n\n" +
			g.Description;

	}


	/// <summary>Builds the breadcrumb bar</summary>
	/// <param name="ParentId">ParentId of the "folder" we've navigated to</param>
	private void BuildBreadcrumbBar(int ParentId)
	{
		pnlBreadcrumb.Controls.Clear();
		pnlBreadcrumb.Controls.Add(new HyperLink() { Text = "Gallery", NavigateUrl = "Gallery.aspx" });
		if (parentId < 1) return;

		// Figure out where we are in the hierarchy
		List<DAL.Gallery> Path = new List<DAL.Gallery>();
		using (DAL.MV8DataContext dc = new DAL.MV8DataContext(System.Configuration.ConfigurationManager.ConnectionStrings["MV8"].ConnectionString)) 
			while (parentId > 0) {
				Path.Add((from g in dc.Galleries where g.id == parentId select g).First());
				parentId = Path[Path.Count-1].ParentId; }

		// Build the breadcrumb bar because the SiteMapPath built into ASP.NET sucks donkey balls. Who needs XML?
		for (int i = Path.Count - 1; i >= 0; i--) {
			pnlBreadcrumb.Controls.Add(new LiteralControl(" &gt; "));
			pnlBreadcrumb.Controls.Add(new HyperLink() { Text = Path[i].DisplayName, NavigateUrl = "Gallery.aspx?ParentId=" + Path[i].id.ToString() }); }
	}
}